﻿Imports System.Windows.Forms
Imports System.Drawing

Module General

    'Public xRefs As New Collections.Generic.Dictionary(Of String, String)
    'Public MyErrs As New Collection
    Public xActiveControl As String
    'Public CLenguage As SelectCompiler.Lenguages
    'Public Project_InstallLibraries As Boolean
    'Public Project_LibrariesFolder As String
    Public ProjectFolder As String
    'Public ActualProject1 As String
    'Public skinCollection As New System.Collections.Generic.Dictionary(Of String, SkinProperty)
    Public OFCDirectory As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
    Public OFCDirectoryInfo As New IO.DirectoryInfo(OFCDirectory)
    Public CurScript As Cintie.ScriptEngine.Script

    Public ScriptDir As IO.DirectoryInfo
    Public CanSaveScript As Boolean = False
    Public ScriptFileName As IO.FileInfo
    Public AppPath As String = My.Application.Info.DirectoryPath

    Sub LoadProject(ByVal File As IO.FileInfo)

        CurScript = New Cintie.ScriptEngine.Script().Load(File)
        ScriptFileName = File

        Try
            Dim nDir As New IO.DirectoryInfo(File.DirectoryName & "\" & File.Name & ".storage")
            If nDir.Exists = False Then
                nDir.Create()
                ScriptDir = nDir
                CanSaveScript = True
            Else
                CanSaveScript = True
                ScriptDir = nDir
            End If

        Catch ex As Exception
            CanSaveScript = False
        End Try

    End Sub

    Public Sub DownloadFile(ByVal UriString As String, ByVal Destination As String)
        Try
            Dim WebClient As New System.Net.WebClient
            Dim buff() As Byte = _
                WebClient.DownloadData(UriString)
            Dim Writer As New IO.StreamWriter(Destination)
            Writer.Write(System.Text.Encoding.UTF8.GetString(buff))
            Writer.Close()
        Catch ex As Exception

        End Try
    End Sub

    Public Sub SaveProject(Optional ByVal File As String = "")
        Try
            Dim _f As String = ""
            If File = "" Then
                _f = ScriptFileName.FullName
            Else
                _f = File
            End If
            Dim lang As String = ""
            Select Case CurScript.Language
                Case Cintie.ScriptEngine.Script.LanguagesList.VisualBasic
                    lang = "VisualBasic"
                Case Cintie.ScriptEngine.Script.LanguagesList.CSharp
                    lang = "CSharp"
            End Select

            Dim Script = <?xml version="1.0" encoding="utf-8"?>
                         <Script>
                             <References>
                                 <%= From ref In CurScript.ReferencesList _
                                     Select <Reference><%= ref.RelativeUri %></Reference> %>
                             </References>
                             <Code Language=<%= lang %>>
                                 <%= New XCData(CurScript.Code) %>
                             </Code>
                         </Script>
            Dim sw As New IO.StreamWriter(_f)
            sw.Write(Script.ToString)
            sw.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub


    Public Function GetColor(ByVal XColor As String) As Color
        With XColor
            Dim A As String = .Chars(0) & .Chars(1)
            Dim R As String = .Chars(2) & .Chars(3)
            Dim G As String = .Chars(4) & .Chars(5)
            Dim B As String = .Chars(6) & .Chars(7)
            Dim Ai As Integer, Ri As Integer
            Dim Gi As Integer, Bi As Integer
            Ai = A
            Ri = R
            Gi = G
            Bi = B
            Dim C As Color = Color.FromArgb(Ai, Ri, Gi, Bi)
            Return (C)

        End With
    End Function

    Public Function GetColor2(ByVal xColor As String) As Color
        Dim C As Color
        Dim Arr() As String = xColor.Split(",")
        Dim vars As New System.Collections.Specialized.StringCollection
        For Each Str As String In Arr
            vars.Add(Str)
        Next
        Try
            Dim A As Integer = vars.Item(0)
            Dim R As Integer = vars.Item(1)
            Dim G As Integer = vars.Item(2)
            Dim B As Integer = vars.Item(3)
            C = Color.FromArgb(A, R, G, B)
        Catch ex As Exception
        End Try
        Return (C)
    End Function

    Public Function ConvertColor(ByVal YColor As Color) As String
        Dim str As String = ""
        With YColor
            str = .A & "," & .R & "," & .G & "," & .B
        End With
        Return str
    End Function

    Public Function FontFromString(ByVal str As String) As Font
        Dim converter As System.ComponentModel.TypeConverter = _
        System.ComponentModel.TypeDescriptor.GetConverter(GetType(Font))
        Dim font1 As Font = _
            CType(converter.ConvertFromString(str), Font)
        Return font1
    End Function


End Module

Public Class SkinStyle_Brush
    Private xColor1 As System.Windows.Media.Color
    Private xColor2 As System.Windows.Media.Color
    Private Gr As Double

    Public Sub New(ByVal fileName As String, ByVal PropertyName As String)
        Dim cnf As New INIFile(fileName)
        Dim c1 As Color = GetColor2(cnf.GetString(PropertyName, "Color1", "255,0,0,0"))
        Dim c2 As Color = GetColor2(cnf.GetString(PropertyName, "Color2", "255,0,0,0"))
        xColor1 = System.Windows.Media.Color.FromArgb(c1.A, c1.R, c1.G, c1.B)
        xColor2 = System.Windows.Media.Color.FromArgb(c2.A, c2.R, c2.G, c2.B)
        Gr = cnf.GetString(PropertyName, "Gradient", "0.5")
    End Sub

    Public Property Color1() As System.Windows.Media.Color
        Get
            Return xColor1
        End Get
        Set(ByVal value As System.Windows.Media.Color)
            xColor1 = value
        End Set
    End Property

    Public Property Color2() As System.Windows.Media.Color
        Get
            Return xColor2
        End Get
        Set(ByVal value As System.Windows.Media.Color)
            xColor2 = value
        End Set
    End Property

    Public Property Gradient() As Double
        Get
            Return Gr
        End Get
        Set(ByVal value As Double)
            Gr = value
        End Set
    End Property

    Public ReadOnly Property ToLinearGradientBrush() As LinearGradientBrush
        Get
            Dim gr As New LinearGradientBrush
            gr.GradientStops.Add(New GradientStop(Color1, 0))
            gr.GradientStops.Add(New GradientStop(Color2, Gradient))
            Return gr
        End Get
    End Property
End Class

Public Class SkinProperty
    Private xWndBackGround As SkinStyle_Brush
    Private xBackGround As SkinStyle_Brush
    Private xForeGround As SkinStyle_Brush
    Private xSkinImage As String
    Private xBackGroundImage As String
    Private xSkinCreator As String
    Private xSkinComments As String
    Private xSkinTitle As String
    Private xSkinDirectory As String

    Public Sub New(ByVal fileName As String, _
                   ByVal BackGroundProperty As String, _
                   ByVal ForeGroundProperty As String)
        Dim conf As New INIFile(fileName)
        xWndBackGround = New SkinStyle_Brush(fileName, BackGroundProperty)
        xBackGround = New SkinStyle_Brush(fileName, BackGroundProperty)
        xForeGround = New SkinStyle_Brush(fileName, ForeGroundProperty)
        xBackGroundImage = conf.GetString("Skin", "BackGroundImage", "null")
        xSkinImage = conf.GetString("skinInfo", "Image", "null")
        xSkinCreator = conf.GetString("skinInfo", "Creator", "null")
        xSkinComments = conf.GetString("skinInfo", "Comments", "null")
        xSkinTitle = conf.GetString("skinInfo", "Title", "null")

    End Sub



    Public Property BackGround() As SkinStyle_Brush
        Get
            Return (xBackGround)
        End Get
        Set(ByVal value As SkinStyle_Brush)
            xBackGround = value
        End Set
    End Property

    Public Property ForeFround() As SkinStyle_Brush
        Get
            Return (xForeGround)
        End Get
        Set(ByVal value As SkinStyle_Brush)
            xForeGround = value
        End Set
    End Property
    Public Property BackGroundImage() As String
        Get
            Return (xBackGroundImage)
        End Get
        Set(ByVal value As String)
            xBackGroundImage = value
        End Set
    End Property

    Public Property SkinImage() As String
        Get
            Return xSkinImage
        End Get
        Set(ByVal value As String)
            xSkinImage = value
        End Set
    End Property

    Public Property SkinCreator() As String
        Get
            Return xSkinCreator
        End Get
        Set(ByVal value As String)
            xSkinCreator = value
        End Set
    End Property

    Public Property SkinComments() As String
        Get
            Return xSkinComments
        End Get
        Set(ByVal value As String)
            xSkinComments = value
        End Set
    End Property

    Public Property SkinTitle() As String
        Get
            Return xSkinTitle
        End Get
        Set(ByVal value As String)
            xSkinTitle = value
        End Set
    End Property

    Public Property Skindirectory() As String
        Get
            Return xSkinDirectory
        End Get
        Set(ByVal value As String)
            xSkinDirectory = value
        End Set
    End Property
End Class

Namespace WPFFunctions
    <HideModuleName()> _
    Module Image
        Function ConvertImage(ByVal src As String, ByVal PixelHeight As Integer, ByVal PixelWidth As Integer) As BitmapImage
            Dim bm1 As New BitmapImage
            bm1.BeginInit()
            Dim uri As New Uri(src)
            bm1.UriSource = uri
            If PixelHeight > 0 Then
                bm1.DecodePixelHeight = PixelHeight
            End If
            If PixelWidth > 0 Then
                bm1.DecodePixelHeight = PixelWidth
            End If
            bm1.EndInit()
            Return bm1
        End Function
    End Module

End Namespace


